package com.wanxi.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.wanxi.dao.NavDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Nav;
import com.wanxi.service.NavService;
import com.wanxi.util.CreateOnlyID;
import com.wanxi.util.JSONCopy;
import com.wanxi.util.RedisTool;
import com.wanxi.vo.NavVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;
@Service
@Transactional
public class NavServiceImpl implements NavService {
    @Autowired
    NavDao navDao;
    @Autowired
    CreateOnlyID createOnlyID;
    @Override
    public ResultDTO findAllNav() {
        List<Nav> list=navDao.findAllNav();
        if(list.size()>0){
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findNavById(NavVO navVO) {
        Nav nav=JSONCopy.copy(navVO, Nav.class);
        Nav n= navDao.findNavById(nav);
        if(n!=null){
            return ResultDTO.getDTO(1,n);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(NavVO navVO) {
        Nav nav=JSONCopy.copy(navVO,Nav.class);
        int i=navDao.updateEnable(nav);
        if(i==1){
            RedisTool.deleteToRedis("nav");
            return ResultDTO.getDTO(i,navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addNav(NavVO navVO) {
        Nav nav=JSONCopy.copy(navVO,Nav.class);
        nav.setId(createOnlyID.createId());
        int i=navDao.addNav(nav);
        if(i==1){
            RedisTool.deleteToRedis("nav");
            return ResultDTO.getDTO(i,navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO update(NavVO navVO) {
        Nav nav=JSONCopy.copy(navVO,Nav.class);
        int i=navDao.updateNav(nav);
        if(i==1){
            RedisTool.deleteToRedis("nav");
            return ResultDTO.getDTO(i,navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO delete(NavVO navVO) {
        Nav nav=JSONCopy.copy(navVO,Nav.class);
        int i=navDao.delete(nav);
        if(i==1){
            RedisTool.deleteToRedis("nav");
            return ResultDTO.getDTO(i,navVO);
        }
        return ResultDTO.builder().data(navVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findNav() {
        List<String> redisNav= RedisTool.findToRedis("nav");
//        判断redis中是否有数据
        if(redisNav!=null){
//            返回redis中的数据,通过fastJson转换
            List<Nav> list= JSON.parseObject(redisNav.toString(), new TypeReference<List<Nav>>() {});
            return ResultDTO.getDTO(list.size(),list);
        }
//        没有则从数据库中找数据
        List<Nav> list=navDao.findAllNav();
        if(list.size()>0){
//            Dao层数据迭代
            Iterator<Nav> iterator=list.iterator();
            while (iterator.hasNext()){
                Nav n= iterator.next();
                if(n.getEnable()==0){
                    iterator.remove();
                }
                else{
//                    将查到的数据放入redis
                    RedisTool.putListToRedis("nav", JSON.toJSONString(n));
                }
            }
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
